﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ExpressionMapper
{
    using  MappingExpressionGenerator = Func<Expression, Type, Expression, ParameterExpression, Expression>;

    public static partial class Generators
    {
        /// <summary>
        /// Returns generator that uses Convert class for mapping.
        /// </summary>
        public static MappingExpressionGenerator UseToString()
        {
            return
                (fromExpr, to, customMapping, instanceDictionary) => (IsString(to))
                                                     ? customMapping ?? GenerateToStringExpression(fromExpr, to)
                                                     : null;
        }

        private static Expression GenerateToStringExpression(Expression fromExpr, Type to)
        {
            Debug.WriteLine(string.Format("ToString ({0}): {1} to {2}", fromExpr.NodeType, fromExpr.Type.Name,
                                  to.Name));

            return Expression.Call(fromExpr, "ToString", null, null);
        }

        private static bool IsString(Type t)
        {
            return t == typeof (string);
        }


    }
}

